<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/2
 * Time: 9:33
 */
namespace app\apiteacher\controller;
use think\Db;
class Boutique extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $user_id2 = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($user_id2,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //精品公开课
    public function index(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $id= input('param.id');
        $where['public_subject'] = $id;
        $where['is_hot'] = 1;
        $res = Db::table('up_public_class')->where($where)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            $count = Db::table('up_public_apply')->where('apply_public_id',$vo['public_id'])->count();
            $vo['num'] = $count;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //课程详情
    public function detail(){
        $where['public_id'] = input('param.id');
        $res = Db::table('up_public_class')->where($where)
            ->find();
        //查询报名人数
        $count = Db::table('up_public_apply')->where('apply_public_id',$res['public_id'])->count();
        $res['num'] = $count;
        //查询老师信息
        $info = Db::table('up_u_user')->where('user_id',$res['public_teacher_id'])
                              ->field('user_id,user_username,user_gender,user_intro,user_grade,
                               user_status,user_image')
                              ->find();
        //星级
        $star = $this->teacherStar($info['user_id']);
        //年级
        $grade = Db::table('up_grade')->where('grade_id',$info['user_grade'])->value('grade_name');
        $res['star'] = $star;
        $res['grade'] = $grade;
        $res['user_gender'] = $info['user_gender'];
        $res['user_intro'] = $info['user_intro'];
        $res['user_status'] = $info['user_status'];
        $res['user_image'] = $info['user_image'];
        return  json(array('status'=>200,'msg'=>$res));
    }

    //课程目录
    public function catalog(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['section_public_id'] = input('param.id');
        $res = Db::table('up_public_section')
            ->where($where)
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //购买课程
    public function buyClass(){
        extract(input());
        $where['public_id'] = input('param.id');
        $res = Db::table('up_public_class')->where($where)
            ->find();
        //查询报名人数
        $count = Db::table('up_public_apply')->where('apply_public_id',$res['public_id'])->count();
        $res['num'] = $count;
        //查询这个用户的平台币
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        $res['user_balance'] = $user_balance;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //确认支付
    public function affirm(){
        extract(input());
        $id = input('param.id');
        $money = input('param.money');
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        if($money>$user_balance){
            return  json(array('status'=>100,'msg'=>"平台币不足,请先充值"));
        }else{
           Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$money);
        }
        //插入用户购买课程表
        $data['apply_user_id'] = $user_id;
        $data['apply_public_id'] = $id;
        $data['apply_time'] = date('Y-m-d H:i:s',time());
        Db::table('up_public_apply')->insertGetId($data);
        //插入订单表
        $data1['order_type'] = 5;
        $data1['order_price'] = $money;
        $data1['order_time'] = date('Y-m-d H:i:s',time());
        $data1['order_user_id'] = $user_id;
        Db::table('up_order')->insertGetId($data1);
        return  json(array('status'=>200,'msg'=>"购买成功"));
    }
}